<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>在线文档的网络层开发思考--依赖关系梳理 | 被删的前端游乐场</title>
    <meta name="generator" content="VuePress 1.8.2">
    
    <meta name="description" content="Just playing around">
    
    <link rel="preload" href="/front-end-playground/assets/css/0.styles.6ad2a9ca.css" as="style"><link rel="preload" href="/front-end-playground/assets/js/app.1e2670bf.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/2.38d016d1.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/3.e3f029cb.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/32.d14a1890.js" as="script">
    <link rel="stylesheet" href="/front-end-playground/assets/css/0.styles.6ad2a9ca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/front-end-playground/" class="home-link router-link-active"><!----> <span class="site-name">被删的前端游乐场</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link router-link-active">前端领域</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/front-end-work/" class="nav-link">前端与工作</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link router-link-active">前端领域</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/front-end-work/" class="nav-link">前端与工作</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0" style="padding-top:10px;"><div class="kitty-main" data-v-2b653b36><span class="stand" data-v-2b653b36></span> <div class="cat" data-v-2b653b36><div class="body" data-v-2b653b36></div> <div class="head" data-v-2b653b36><div class="ear" data-v-2b653b36></div> <div class="ear" data-v-2b653b36></div></div> <div class="face" data-v-2b653b36><div class="nose" data-v-2b653b36></div> <div class="whisker-container" data-v-2b653b36><div class="whisker" data-v-2b653b36></div> <div class="whisker" data-v-2b653b36></div></div> <div class="whisker-container" data-v-2b653b36><div class="whisker" data-v-2b653b36></div> <div class="whisker" data-v-2b653b36></div></div></div> <div class="tail-container" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36><div class="tail" data-v-2b653b36></div></div></div></div></div></div></div></div></div></div> <p class="sidebar-heading open"><span>前端架构</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/front-end-playground/front-end-basic/deep-learning/module-seperate.html" class="sidebar-link">谈谈依赖和解耦</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/trace-stash.html" class="sidebar-link">大型前端项目要怎么跟踪和分析函数调用链</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/build-application.html" class="sidebar-link">前端构建大型应用</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/reactive-programing.html" class="sidebar-link">响应式编程在前端领域的应用</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/vscode-event.html" class="sidebar-link">VSCode 源码解读：事件系统设计</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/vscode-ipc.html" class="sidebar-link">VSCode 源码解读：IPC通信机制</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/online-doc-network.html" class="sidebar-link">在线文档的网络层设计思考</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/front-end-performance-analyze.html" class="sidebar-link">补齐Web前端性能分析的工具盲点</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/monitor-and-report.html" class="sidebar-link">前端监控体系搭建</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/why-spreadsheet-app-excited.html" class="sidebar-link">在线Excel项目到底有多刺激</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/task-runner-design.html" class="sidebar-link">如何设计一个任务管理器</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/network-design-responsibility-driven-design.html" class="sidebar-link">在线文档的网络层开发思考--职责驱动设计</a></li><li><a href="/front-end-playground/front-end-basic/deep-learning/network-design-dependency-decoupling.html" aria-current="page" class="active sidebar-link">在线文档的网络层开发思考--依赖关系梳理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-basic/deep-learning/network-design-dependency-decoupling.html#依赖关系梳理" class="sidebar-link">依赖关系梳理</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-basic/deep-learning/network-design-dependency-decoupling.html#总控制器的职责梳理" class="sidebar-link">总控制器的职责梳理</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-basic/deep-learning/network-design-dependency-decoupling.html#结束语" class="sidebar-link">结束语</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>前端深入理解</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>前端入门</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>最近在负责通用网络层的设计和开发，会记录该过程中的一些思考，本文主要介绍接入层设计过程中的一些依赖关系，以及处理这些依赖关系的一些思考。</p> <p>在<a href="/front-end-playground/front-end-basic/deep-learning/network-design-responsibility-driven-design.html">上一篇</a>文章中，我尝试使用职责驱动设计来重新梳理了接入层的职责对象，最终得到了这样的依赖关系图：</p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/network-design-8.jpg" alt=""></p> <p>这里的依赖关系表示得很简单，实际上这样简单的表示是无法完成代码开发的，我们还需要根据每个对象的职责将它们之间的协作方式整理出来，可以通过接口或者 UML 图的方式来进行。</p> <h2 id="依赖关系梳理"><a href="#依赖关系梳理" class="header-anchor">#</a> 依赖关系梳理</h2> <p>技术方案设计离不开业务，我们开发的很多工具和 SDK 最终也是服务与业务，因此我们首先需要梳理出网络层与业务侧的一些依赖关系，从而可得到更加明确的职责范围。</p> <h3 id="梳理网络层与业务侧依赖"><a href="#梳理网络层与业务侧依赖" class="header-anchor">#</a> 梳理网络层与业务侧依赖</h3> <p>原先的网络层由于历史原因与业务中其他模块耦合严重，其中网络层的代码中对其他模块（包括数据层、离线模块、worker 模块等）的直接引用以及使用事件通信多达 50+处。因此，如果希望重构后的网络层能正常在业务中使用，我们首先需要将相关依赖全部梳理出来，确认是否可通过适配层的方式进行解耦，让网络层专注于自身的职责功能。</p> <p>经过梳理，我们整理出网络层的与业务层的主要依赖关系，包括：</p> <ol><li>业务侧为主动方时：</li></ol> <ul><li>业务侧将数据提交到网络层</li> <li>业务侧可控制网络层工作状态，可用于预防异常的情况</li> <li>业务侧主动获取网络层自身的一些状态，包括网络层是否正确运行、网络层状态（在线/离线）等</li></ul> <ol start="2"><li>业务侧为被动方时：</li></ol> <ul><li>网络层告知业务侧，需要进行数据冲突处理</li> <li>网络层告知业务侧服务端的最新状态，包括数据是否递交成功、是否有新的服务端消息等</li> <li>网络层告知业务侧自身的一些状态变更，包括网络层状态变更（异常/挂起）、网络层工作是否存在异常等</li></ul> <p>除此之外，网络层初始化也依赖一些业务侧的数据，包括初始版本信息、用户登录态、文档 ID 等等。</p> <p>到这里，我们可以根据这些依赖关系，简化网络层与业务侧的关系：</p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/network-design-1.jpg" alt=""></p> <p>能看到，简化后的网络层与业务侧关系主要包括三种：</p> <ol><li>业务侧初始化网络层。</li> <li>业务侧给网络层提交数据，以及控制网络层的工作状态。</li> <li>业务侧监听网络层的状态变更。</li></ol> <p>前面我们也说了，业务侧与网络层的协作主要通过接入层的总控制器来完成，也就是说总控制器的职责和协作方式包括：</p> <ol><li>初始化整个网络层，创建网络层运行需要的各个协作对象，在这里总控制器也可视作创建者（creator）。</li> <li>通过提供接口的方式，对业务层提供数据提交（<code>addData()</code>）和控制网络层状态（<code>pause()</code>/<code>resume()</code>/<code>shutdown()</code>）的方法。</li> <li>通过提供事件监听的方式，对业务层提供网络层的各种状态变更（<code>onNetworkChange()</code>/<code>onDataCommitSuccess()</code>/<code>onDataCommitError()</code>/<code>onNewData()</code>）。</li></ol> <p>具体网络层中总控制器是如何调度其他对象进行协作的，这些细节不需要暴露给业务侧。在对齐了业务侧的需要之后，我们再来看看具体网络层的细节。</p> <h2 id="总控制器的职责梳理"><a href="#总控制器的职责梳理" class="header-anchor">#</a> 总控制器的职责梳理</h2> <p>对业务侧来说，它只关注和网络层的协作，不关注具体网络层中接入层和连接层的关系。而对于接入层来说，其实它对连接层有直接的层级关系，因此这里我们将连接层以及服务端视作一个单独的职责对象：</p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/network-design-9.jpg" alt=""></p> <p>实际上这些模块之间的依赖关系比这些还要复杂得多，比如发送数据控制器和接受数据控制器都会直接依赖连接层。为了方便描述，这里我们就不纠结这些细节了。</p> <h3 id="初始化"><a href="#初始化" class="header-anchor">#</a> 初始化</h3> <p>前面也说了，总控制器需要负责整个网络层的初始化，因此它需要控制各个职责对象的创建。那么，图中发送数据控制器和接受数据控制器对其他对象的依赖，可以通过初始化控制器对象时注入的方式来进行控制。</p> <p>如果是注入的方式，则这样的依赖关系可描述为对接口的依赖，我们用虚线进行标记：</p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/network-design-10.jpg" alt=""></p> <p>其中虚线的地方，都可以理解为初始化时需要注入的依赖对象。初始化相关的代码大致会长这样：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">class</span> <span class="token class-name">NetworkController</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token parameter">options<span class="token operator">:</span> INetworkControllerOptions</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>versionManager <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VersionManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 版本管理</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>connectLayer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConnectLayer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 连接层</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>netWorkManager <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NetWorkManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 网络状态管理</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>taskListManager <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TaskListManager</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>versionManager<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 任务队列管理</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>dataListManager <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DataListManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 待提交数据队列</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>sendDataController <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SendDataController</span><span class="token punctuation">(</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>taskListManager<span class="token punctuation">,</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>dataListManager
    <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 发送数据控制器</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>receiveDataController <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReceiveDataController</span><span class="token punctuation">(</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>taskListManager<span class="token punctuation">,</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>dataListManager<span class="token punctuation">,</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>netWorkManager
    <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 接受数据控制器</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>这里虽然我们传入了实例对象，但在对象内部，依赖的对象除了是实例，还可以是抽象的接口。</p> <h4 id="使用依赖倒置进行依赖解耦"><a href="#使用依赖倒置进行依赖解耦" class="header-anchor">#</a> 使用依赖倒置进行依赖解耦</h4> <p>依赖倒置原则有两个，其中包括了：</p> <ol><li>高层次的模块不应该依赖于低层次的模块，两者都应该依赖于抽象接口。</li> <li>抽象接口不应该依赖于具体实现，而具体实现则应该依赖于抽象接口。</li></ol> <p>以<code>SendDataController</code>为例，它依赖<code>TaskListManager</code>其实主要是依赖的添加任务的接口<code>addTask()</code>，依赖<code>DataListManager</code>则是依赖添加数据<code>pushData()</code>、取出数据<code>shiftData()</code>，则我们可以表达为：</p> <div class="language-typescript extra-class"><pre class="language-typescript"><code><span class="token keyword">interface</span> <span class="token class-name">ITaskListManagerDependency</span> <span class="token punctuation">{</span>
  <span class="token function-variable function">addTask</span><span class="token operator">:</span> <span class="token punctuation">(</span>task<span class="token operator">:</span> BaseTask<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">interface</span> <span class="token class-name">IDataListManagerDependency</span> <span class="token punctuation">{</span>
  <span class="token function-variable function">pushData</span><span class="token operator">:</span> <span class="token punctuation">(</span>data<span class="token operator">:</span> LocalData<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
  <span class="token function-variable function">shiftData</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> LocalData<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">class</span> <span class="token class-name">SendDataController</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span>
    taskListManagerDependency<span class="token operator">:</span> ITaskListManagerDependency<span class="token punctuation">,</span>
    dataListManagerDependency<span class="token operator">:</span> IDataListManagerDependency
  <span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 相关依赖可以保存起来，在需要的时候使用</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>实际上，我们可以给每个对象提供自身的接口描述，这样其他对象中可以直接<code>import</code>同一份接口也是可以的，管理和调整会比较方便。</p> <p>如果项目中有完善的依赖注入框架，则可以使用项目中的依赖注入体系。在我们这个例子里，总控制器充当了依赖注入的控制角色，而具体其中的各个对象之间，实现了基于抽象接口的依赖，成功了进行了解耦。依赖注入在大型项目中比较常见，对于各个模块间的依赖关系管理很实用。</p> <h3 id="提供接口和事件监听"><a href="#提供接口和事件监听" class="header-anchor">#</a> 提供接口和事件监听</h3> <p>除了初始化相关，总控制器的职责还包括对业务层提供接口和事件监听，其中接口中会依赖具体职责对象的协作：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">class</span> <span class="token class-name">NetworkController</span> <span class="token punctuation">{</span>
    <span class="token comment">// 提供的接口</span>
    <span class="token function">addData</span><span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> ILocalData</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>sendDataController<span class="token punctuation">.</span><span class="token function">addData</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">pause</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>taskListManager<span class="token punctuation">.</span><span class="token function">pause</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>taskListManager<span class="token punctuation">.</span><span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>taskListManager<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>connectLayer<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>在最初的设计中，我们的状态变更这些也是通过注册回调的方式进行设计的：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">interface</span> <span class="token class-name">INetworkControllerOptions</span> <span class="token punctuation">{</span>
    <span class="token comment">// 其他参数</span>
    <span class="token function-variable function">onNetworkChange</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">newStatus<span class="token operator">:</span> NetWorkStatus</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">,</span>
    <span class="token function-variable function">onDataCommitSuccess</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> LocalData</span><span class="token punctuation">)</span>  <span class="token operator">=&gt;</span> <span class="token keyword">void</span>
    <span class="token function-variable function">onDataCommitError</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> LocalData</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span>
    <span class="token function-variable function">onNewData</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> ServerData</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">NetworkController</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token parameter">options<span class="token operator">:</span> INetworkControllerOptions</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 需要将各个接口实现保存下来</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>这种方式意味着我们需要将这些接口实现保存下来，并传入到各个对象内部分别在恰当的时机进行调用，调用的时候还需要关注是否出现异常，同样以<code>SendDataController</code>为例：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">interface</span> <span class="token class-name">ICallbackDependency</span> <span class="token punctuation">{</span>
    onDataCommitSuccess<span class="token operator">?</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> LocalData</span><span class="token punctuation">)</span>  <span class="token operator">=&gt;</span> <span class="token keyword">void</span>
    onDataCommitError<span class="token operator">?</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> LocalData</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span>
<span class="token punctuation">}</span>
<span class="token keyword">interface</span> <span class="token class-name">ITaskListManagerDependency</span> <span class="token punctuation">{</span>
  <span class="token function-variable function">addTask</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">task<span class="token operator">:</span> BaseTask</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">interface</span> <span class="token class-name">IDataListManagerDependency</span> <span class="token punctuation">{</span>
  <span class="token function-variable function">pushData</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> LocalData</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
  <span class="token function-variable function">shiftData</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> LocalData<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">class</span> <span class="token class-name">SendDataController</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span>
    taskListManagerDependency<span class="token operator">:</span> ITaskListManagerDependency<span class="token punctuation">,</span>
    dataListManagerDependency<span class="token operator">:</span> IDataListManagerDependency<span class="token punctuation">,</span>
    <span class="token comment">// 在初始化的时候需要通过注入的方式传进来</span>
    callbackDependency<span class="token operator">:</span> ICallbackDependency<span class="token punctuation">,</span>
  <span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

  <span class="token function">handleDataCommitSuccess</span><span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> LocalData</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">try</span> <span class="token punctuation">{</span>
          <span class="token comment">// 还该函数还可能为空</span>
          <span class="token keyword">this</span><span class="token punctuation">.</span>callbackDependency<span class="token punctuation">.</span>onDataCommitSuccess<span class="token operator">?.</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token comment">// 使用的时候还需要注意异常问题</span>
      <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>除此之外，这种方式还导致了业务侧在使用的时候，初始化就要传入很多的接口实现：</p> <div class="language-typescript extra-class"><pre class="language-typescript"><code><span class="token keyword">const</span> netWorkLayer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NetworkController</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token comment">// 其他参数</span>
    otherOptions<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    onNetworkChange<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 网络状态变更处理</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    onDataCommitSuccess<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 提交数据成功处理</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    onDataCommitError<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 提交数据失败处理</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    onNewData<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 服务端新数据处理</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>可以看到，业务侧中初始化网络层的代码特别长（传入了 20 多个方法），实际上在不同的业务中这些接口可能是不必要的。</p> <h4 id="使用事件驱动进行依赖解耦"><a href="#使用事件驱动进行依赖解耦" class="header-anchor">#</a> 使用事件驱动进行依赖解耦</h4> <p>在这里，我们使用了事件处理模型-观察者模式。事件驱动其实常常在各种系统设计中会用到，可以解耦目标对象和它的依赖对象。目标只需要通知它的依赖对象，具体怎么处理，依赖对象自己决定。</p> <p>事件监听的实现，参考了<a href="/front-end-playground/front-end-basic/deep-learning/vscode-event.html">VsCode 的事件系统设计</a>的做法，比如在<code>SendDataController</code>中：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">class</span> <span class="token class-name">SendDataController</span> <span class="token punctuation">{</span>
  <span class="token keyword">private</span> readonly _onDataCommitSuccess <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Emitter</span><span class="token operator">&lt;</span>LocalData<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  readonly onDataCommitSuccess<span class="token operator">:</span> Event<span class="token operator">&lt;</span>LocalData<span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>_onDataCommitSuccess<span class="token punctuation">.</span>event<span class="token punctuation">;</span>

  <span class="token function">constructor</span><span class="token punctuation">(</span>
    taskListManagerDependency<span class="token operator">:</span> ITaskListManagerDependency<span class="token punctuation">,</span>
    dataListManagerDependency<span class="token operator">:</span> IDataListManagerDependency<span class="token punctuation">,</span>
    <span class="token comment">// 在初始化的时候需要通过注入的方式传进来</span>
    callbackDependency<span class="token operator">:</span> ICallbackDependency<span class="token punctuation">,</span>
  <span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

  <span class="token function">handleDataCommitSuccess</span><span class="token punctuation">(</span><span class="token parameter">data<span class="token operator">:</span> LocalData</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>_onDataCommitSuccess<span class="token punctuation">.</span><span class="token function">fire</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>在总控制器中，可以同样通过事件监听的方式传递出去：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">class</span> <span class="token class-name">NetworkController</span> <span class="token punctuation">{</span>
    <span class="token comment">// 提供的事件</span>
    <span class="token keyword">private</span> readonly _onNetworkChange <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Emitter</span><span class="token operator">&lt;</span>NetWorkStatus<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    readonly onNetworkChange<span class="token operator">:</span> Event<span class="token operator">&lt;</span>NetWorkStatus<span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>_onNetworkChange<span class="token punctuation">.</span>event<span class="token punctuation">;</span>

    <span class="token keyword">private</span> readonly _onDataCommitSuccess <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Emitter</span><span class="token operator">&lt;</span>LocalData<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    readonly onDataCommitSuccess<span class="token operator">:</span> Event<span class="token operator">&lt;</span>LocalData<span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>_onDataCommitSuccess<span class="token punctuation">.</span>event<span class="token punctuation">;</span>

    <span class="token keyword">private</span> readonly _onNewData <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Emitter</span><span class="token operator">&lt;</span>ServerData<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    readonly onNewData<span class="token operator">:</span> Event<span class="token operator">&lt;</span>ServerData<span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>_onNewData<span class="token punctuation">.</span>event<span class="token punctuation">;</span>

    <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token parameter">options<span class="token operator">:</span> INetworkControllerOptions</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">initEvent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">initEvent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 监听 SendDataController 的事件，并触发自己的事件</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>sendDataController<span class="token punctuation">.</span><span class="token function">onDataCommitSuccess</span><span class="token punctuation">(</span><span class="token parameter">data</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>_onDataCommitSuccess<span class="token punctuation">.</span><span class="token function">fire</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>使用事件监听的方式，业务方就可以在需要的地方再进行监听了：</p> <div class="language-typescript extra-class"><pre class="language-typescript"><code><span class="token keyword">const</span> netWorkLayer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NetworkController</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token comment">// 其他参数</span>
  otherOptions<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 网络状态变更处理</span>
netWorkLayer<span class="token punctuation">.</span><span class="token function">onNetworkChange</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 服务端新数据处理</span>
netWorkLayer<span class="token punctuation">.</span><span class="token function">onNewData</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>到这里，我们可以简单实现了总控制器的职责，也通过接口和事件监听的方式提供了与外界的协作方式，简化了业务侧的使用过程。</p> <h2 id="结束语"><a href="#结束语" class="header-anchor">#</a> 结束语</h2> <p>在本文中，主要根据业务侧与网络层的依赖关系，清晰地梳理出总控制器的职责和协作方式，并尝试对其中的依赖关系进行解耦。</p> <p>而具体到网络层中每个对象的设计和实现，也都是可以通过接口的方式提供给外部使用某些功能、通过事件监听的方式提供给外部获取状态变更。而恰当地使用依赖倒置原则和事件驱动的方式，可以有效地解耦对象间的依赖。</p></div> <!----> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/godbasin/front-end-playground/edit/sourcecode/docs/front-end-basic/deep-learning/network-design-dependency-decoupling.md" target="_blank" rel="noopener noreferrer">帮阿猪改善此页面！</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----> <blockquote>部分文章中使用了一些网站的截图，如果涉及侵权，请告诉我删一下谢谢~</blockquote> <div style="margin-top:30px;"><div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-sm-0 el-col-md-2 el-col-lg-4" style="padding-left:10px;padding-right:10px;display:block;"><div style="width:1px;height:1px;"></div></div> <div class="el-col el-col-24 el-col-sm-24 el-col-md-18 el-col-lg-16" style="padding-left:10px;padding-right:10px;"><div class="el-card box-card is-always-shadow"><div class="el-card__header"><div class="clearfix"><span>温馨提示喵</span></div></div><div class="el-card__body"> <div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="el-image"><div class="image-slot"><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/loading.gif" style="width:100%;"></div><!----></div></div> <div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="copyright-text"><div>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div> <div>出处：被删的前端游乐场</div> <div>作者：<a href="https://github.com/godbasin" target="_blank">被删</a></div></div></div></div></div></div></div></div></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/front-end-playground/front-end-basic/deep-learning/network-design-responsibility-driven-design.html" class="prev">
          在线文档的网络层开发思考--职责驱动设计
        </a></span> <span class="next"><a href="/front-end-playground/front-end-basic/understanding/operation-transform.html">
          多人协作如何进行冲突处理
        </a>
        →
      </span></p></div>  <div class="gitalk-container theme-default-content"><div id="gitalk-container" class="content"></div></div></main> <div id="kitty-container"><span><div role="tooltip" id="el-popover-5813" aria-hidden="true" class="el-popover el-popper" style="width:undefinedpx;display:none;"><!----><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" class="image"> <div class="text">牡羊猪的猫粮罐</div> </div><span class="el-popover__reference-wrapper"><div id="kitty" style="background:url(https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/kitty2.svg);"></div></span></span> <div class="el-dialog__wrapper" style="display:none;"><div role="dialog" aria-modal="true" aria-label="牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）" class="el-dialog" style="margin-top:15vh;"><div class="el-dialog__header"><span class="el-dialog__title">牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）</span><button type="button" aria-label="Close" class="el-dialog__headerbtn"><i class="el-dialog__close el-icon el-icon-close"></i></button></div><!----><!----></div></div></div></div><div class="global-ui"></div></div>
    <script src="/front-end-playground/assets/js/app.1e2670bf.js" defer></script><script src="/front-end-playground/assets/js/2.38d016d1.js" defer></script><script src="/front-end-playground/assets/js/3.e3f029cb.js" defer></script><script src="/front-end-playground/assets/js/32.d14a1890.js" defer></script>
  </body>
</html>
